package org.gwtunclutter.client;

import com.google.gwt.user.client.ui.*;

public class WidgetFactory {

  public static Image image(String url, int width, int height, String style) {
    Image img = new Image(url);
    img.setPixelSize(width, height);
    if (style != null && style.length() > 0)
      img.addStyleName(style);
    return img;
  }

  // factory methods for covenience

  public static HorizontalPanel horizontalPanel(Widget w) {
    HorizontalPanel p = new HorizontalPanel();
    p.add(w);
    return p;
  }

  public static HorizontalPanel horizontalPanel(Widget[] widgets) {
    HorizontalPanel p = new HorizontalPanel();
    for (int i = 0; i < widgets.length; i++)
      p.add(widgets[i]);
    return p;
  }


  public static HorizontalPanel horizontalPanel(int spacing, Widget w) {
    HorizontalPanel p = horizontalPanel(w);
    p.add(w);
    p.setSpacing(spacing);
    return p;
  }

  // TODO: extract spacing values into CellPanelStyle
  public static HorizontalPanel horizontalPanel(int spacing, Widget[] widgets) {
    HorizontalPanel p = horizontalPanel(widgets);
    p.setSpacing(spacing);
    return p;
  }

  public static Grid grid(Widget[][] widgets) {
    Grid grid = new Grid(widgets.length, widgets[0].length);
    // add the widgets
    for (int i = 0; i < widgets.length; i++) {
      for (int j = 0; j < widgets[i].length; j++) {
        grid.setWidget(i, j, widgets[i][j]);
      }
    }
    return grid;
  }

  public static Grid grid(int cellSpacing, Widget[][] widgets) {
    Grid grid = grid(widgets);
    grid.setCellSpacing(cellSpacing);
    return grid;
  }

  public static VerticalPanel verticalPanel(Widget w) {
    VerticalPanel p = new VerticalPanel();
    p.add(w);
    return p;
  }

  public static VerticalPanel verticalPanel(Widget[] widgets) {
    VerticalPanel p = new VerticalPanel();
    for (int i = 0; i < widgets.length; i++)
      p.add(widgets[i]);
    return p;
  }


  public static VerticalPanel verticalPanel(int spacing, Widget w) {
    VerticalPanel p = verticalPanel(w);
    p.add(w);
    p.setSpacing(spacing);
    return p;
  }

  public static VerticalPanel verticalPanel(int spacing, Widget[] widgets) {
    VerticalPanel p = verticalPanel(widgets);
    p.setSpacing(spacing);
    return p;
  }

  public static DisclosurePanel disclosurePanel(String headerText, boolean startOpened, Widget content) {
    DisclosurePanel dp = new DisclosurePanel(headerText, startOpened);
    dp.setContent(content);
    return dp;
  }

  public static DisclosurePanel disclosurePanel(final AbstractImagePrototype[] images, String headerText, boolean startOpened, Widget content) {
    DisclosurePanel dp = new DisclosurePanel(new DisclosurePanelImages() {
      public AbstractImagePrototype disclosurePanelOpen() {
        return images[0];
      }
      public AbstractImagePrototype disclosurePanelClosed() {
        return images[1];
      }
    }, headerText, startOpened);
    dp.setContent(content);
    return dp;
  }

  public static DisclosurePanel disclosurePanel(Widget header, boolean startOpened, Widget content) {
    DisclosurePanel dp = new DisclosurePanel(header, startOpened);
    dp.setContent(content);
    return dp;
  }

  public static TextBox textBox(int visibleChars, int maxChars) {
    TextBox txt = new TextBox();
    txt.setVisibleLength(visibleChars);
    txt.setMaxLength(maxChars);
    return txt;
  }

  public static TextBox textBox(int visibleChars, int maxChars, String text) {
    TextBox txt = textBox(visibleChars, maxChars);
    txt.setText(text);
    return txt;
  }

  public static CheckBox checkBox(String label, boolean checked) {
    return checkBox(label, false, checked);
  }

  public static CheckBox checkBox(String label, boolean asHTML, boolean checked) {
    CheckBox chk = new CheckBox(label, asHTML);
    chk.setChecked(checked);
    return chk;
  }

  /**
   * Creates a simple panel initially containing the given widget
   */
  public static SimplePanel simplePanel(Widget widget) {
    SimplePanel sp = new SimplePanel();
    sp.setWidget(widget);
    return sp;
  }

  public static Label label(String text, String styleName) {
    return label(text, styleName, true);
  }

  public static Label label(String text, String styleName, boolean wordWrap) {
    Label l = new Label(text, wordWrap);
    l.setStyleName(styleName);
    return l;
  }

  public static HTML html(String html, String styleName) {
    return html(html, styleName, true);
  }

  public static HTML html(String html, String styleName, boolean wordWrap) {
    HTML h = new HTML(html, wordWrap);
    h.setStyleName(styleName);
    return h;
  }

  public static SimplePanel simplePanel(WidgetStyle style, Widget widget) {
    SimplePanel sp = simplePanel(widget);
    style.apply(sp);
    return sp;
  }

  public static VerticalPanel verticalPanel(WidgetStyle style, Widget[] widgets) {
    VerticalPanel vp = verticalPanel(widgets);
    style.apply(vp);
    return vp;
  }

  public static HorizontalPanel horizontalPanel(WidgetStyle style, Widget[] widgets) {
    HorizontalPanel hp = horizontalPanel(widgets);
    style.apply(hp);
    return hp;
  }

  public static HorizontalPanel horizontalPanel(int spacing, WidgetStyle style, Widget[] widgets) {
    HorizontalPanel hp = horizontalPanel(spacing, widgets);
    style.apply(hp);
    return hp;
  }

  public static ScrollPanel scrollPanel(WidgetStyle style, Widget child) {
    ScrollPanel sp = new ScrollPanel(child);
    style.apply(sp);
    return sp;
  }

  public static VerticalPanel verticalPanel(int spacing, WidgetStyle style, Widget[] widgets) {
    VerticalPanel vp = verticalPanel(spacing, widgets);
    style.apply(vp);
    return vp;
  }

  public static TabPanel tabPanel(Widget[] widgets, String[] captions, int selectedTabIndex) {
    TabPanel tp = new TabPanel();
    for (int i = 0; i < widgets.length; i++) {
      tp.add(widgets[i], captions[i]);
    }
    tp.selectTab(selectedTabIndex);
    return tp;
  }
}
